package com.iflytek.uc.service.impl;

import java.util.List;
import java.util.Map;

import com.iflytek.uc.dao.AccountDao;
import com.iflytek.uc.domain.Account;
import com.iflytek.uc.domain.Entity;
import com.iflytek.uc.exceptions.AccoutDisabledException;
import com.iflytek.uc.exceptions.DuplicateKeyException;
import com.iflytek.uc.exceptions.NoSuchAccountException;
import com.iflytek.uc.exceptions.PasswordErrorException;
import com.iflytek.uc.service.AccountService;
import com.iflytek.uc.service.ConfigService;

public class AccountServiceImpl implements AccountService {
	
	private AccountDao accountDao;
	private ConfigService configService;

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	public void setConfigService(ConfigService configService) {
		this.configService = configService;
	}

	public Account doLogin(String name, String password)
			throws NoSuchAccountException, AccoutDisabledException,
			PasswordErrorException {
		
		Account account = accountDao.getAccountByName(name);
		
		if (account == null) {
			throw new NoSuchAccountException();
		}
		
		if (account.isDisabled()) {
			throw new AccoutDisabledException();
		}
		
		boolean authenticate = configService.getAuthenticationHandler().authenticate(account, password);
		if (!authenticate) {
			throw new PasswordErrorException();
		}
		
		return account;
	}

	public boolean doAuthenticate(String name, String password) {
		// TODO Auto-generated method stub
		return false;
	}

	public void updatePassword(String name, String password) {
		// TODO Auto-generated method stub

	}

	public Account createAccount(String name, String displayName,
			int displayOrder, String password, Map<String, Object> properties,
			List<Entity> parents, List<Entity> children)
			throws DuplicateKeyException {
		// TODO Auto-generated method stub
		return null;
	}

	public void updateAccount(String name, String displayName,
			int displayOrder, String password, boolean disabled,
			Map<String, Object> properties, List<Entity> parents,
			List<Entity> children) throws DuplicateKeyException {
		// TODO Auto-generated method stub

	}

	public void deleteAccount(String name) {
		// TODO Auto-generated method stub

	}

}
